package pl.skubicha.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import pl.skubicha.domain.Users;
import pl.skubicha.exceptions.UserNotFoundException;

public interface UserRepository extends JpaRepository< Users, Integer > {

	public final static String FIND_ALL_TEACHERS_QUERY = "SELECT g FROM Users g WHERE g.roles.roleName = 'Wykładowca' ";
	public final static String FIND_ALL_TEACHERS_BY_SEARCHSTRING_QUERY = FIND_ALL_TEACHERS_QUERY + " and ( g.firstName like :searchString or g.lastName like :searchString or g.login like :searchString or g.email like :searchString)";
	public final static String FIND_ALL_STUDENTS_QUERY = "SELECT g FROM Users g WHERE g.roles.roleName = 'Student' ";
	public final static String FIND_ALL_STUDENTS_BY_SEARCHSTRING_QUERY = FIND_ALL_TEACHERS_QUERY + " and ( g.firstName like :searchString or g.lastName like :searchString or g.login like :searchString or g.email like :searchString)";
	public final static String FIND_ALL_STUDENTS_BY_GROUP_QUERY = " SELECT u FROM Users u INNER JOIN u.groups g WHERE g.id = :id";
	public final static String FIND_ALL_STUDENTS_BY_GROUP_AND_SUBJECT_QUERY = " SELECT u FROM Users u JOIN u.groups g JOIN u.subjects s WHERE g.id = :idGroup and s.id = :idSubject";

	Users findByLogin( String login ) throws UserNotFoundException;

	@Query( FIND_ALL_TEACHERS_QUERY )
	List< Users > findAllTeachers();

	@Query( FIND_ALL_TEACHERS_QUERY )
	Page< Users > findAllTeachers( Pageable pageable );

	@Query( FIND_ALL_TEACHERS_BY_SEARCHSTRING_QUERY )
	Page< Users > findAllTeachersBySearchString( @Param( "searchString" ) String searchString, Pageable pageable );

	@Query( FIND_ALL_STUDENTS_QUERY )
	List< Users > findAllStudents();

	@Query( FIND_ALL_STUDENTS_QUERY )
	Page< Users > findAllStudents( Pageable pageable );

	@Query( FIND_ALL_STUDENTS_BY_SEARCHSTRING_QUERY )
	Page< Users > findAllStudentsBySearchString( @Param( "searchString" ) String searchString, Pageable pageable );

	@Query( FIND_ALL_STUDENTS_BY_GROUP_QUERY )
	Page< Users > findAllStudentsInGroup( @Param( "id" ) Integer id, Pageable pageable );

	@Query( FIND_ALL_STUDENTS_BY_GROUP_QUERY )
	List< Users > findAllStudentsInGroup( @Param( "id" ) Integer id );
	
	@Query( FIND_ALL_STUDENTS_BY_GROUP_AND_SUBJECT_QUERY )
	List< Users > findAllStudentsInGroupAndSubject( @Param( "idGroup" ) Integer idGroup,  @Param( "idSubject" ) Integer idSubject );
}
